package com.acwing.partition12;

import java.io.*;
import java.util.LinkedList;

/**
 * @author `RKC`
 * @date 2021/12/26 11:42
 */
public class AC1106山峰和山谷 {

    private static final int N = 1010;
    private static final int[][] graph = new int[N][N];
    private static final boolean[][] visited = new boolean[N][N];

    private static int n = 0;

    private static final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    private static final BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));

    public static void main(String[] args) throws IOException {
        String[] s = reader.readLine().split("\\s+");
        n = Integer.parseInt(s[0]);
        for (int i = 0; i < n; i++) {
            s = reader.readLine().split("\\s+");
            for (int j = 0; j < n; j++) graph[i][j] = Integer.parseInt(s[j]);
        }
        resolve();
        writer.flush();
    }

    private static void resolve() throws IOException {
        int peak = 0, valley = 0;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                if (visited[i][j]) continue;
                //返回值是一个二元组 (hasHigher, hasLower)，也可以在外部定义一个二元组数组或者对象进行引用传递得到返回值
                boolean[] result = bfs(i, j);
                if (!result[0]) peak++;
                if (!result[1]) valley++;
            }
        }
        writer.write(peak + " " + valley);
    }

    private static boolean[] bfs(int x, int y) {
        boolean[] result = {false, false};
        LinkedList<int[]> queue = new LinkedList<>();
        queue.addLast(new int[]{x, y});
        visited[x][y] = true;
        while (!queue.isEmpty()) {
            int[] curr = queue.pollFirst();
            int currX = curr[0], currY = curr[1];
            for (int i = currX - 1; i <= currX + 1; i++) {
                for (int j = currY - 1; j <= currY + 1; j++) {
                    if (i == currX && j == currY) continue;
                    if (i < 0 || i >= n || j < 0 || j >= n) continue;
                    if (graph[i][j] > graph[x][y]) result[0] = true;
                    else if (graph[i][j] < graph[x][y]) result[1] = true;
                    else if (!visited[i][j]) {
                        queue.addLast(new int[]{i, j});
                        visited[i][j] = true;
                    }
                }
            }
        }
        return result;
    }
}
